﻿namespace OleDbDAL.Stock
{
    using DBUtility;
    using IDal.Stock;
    using Model.Stock;
    using System;
    using System.Data;
    using System.Text;

    public class InventoryApp : IInventory
    {
        private string dbname = "Systems";

        public void Add(InventoryEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_Inventory(");
            builder.Append("ChainNum,InventNum,UserId,InventTime,IsCheck,CheckMan,CheckTime,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.InventNum + "',");
            builder.Append("'" + model.UserId + "',");
            builder.Append("'" + model.InventTime + "',");
            builder.Append("" + model.IsCheck + ",");
            builder.Append("" + model.CheckMan + ",");
            builder.Append("'" + model.CheckTime + "',");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int InventoryId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_Inventory ");
            builder.Append(" where InventoryId=" + InventoryId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Inventory ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by InventoryId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "InventoryId", "T_Inventory", swhere);
        }

        public InventoryEnt GetModel(int InventoryId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select InventoryId,ChainNum,InventNum,UserId,InventTime,IsCheck,CheckMan,CheckTime,Remark from T_Inventory ");
            builder.Append(" where InventoryId=" + InventoryId);
            InventoryEnt ent = new InventoryEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.InventoryId = InventoryId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.InventNum = table.Rows[0]["InventNum"].ToString();
                ent.UserId = table.Rows[0]["UserId"].ToString();
                if (table.Rows[0]["InventTime"].ToString() != "")
                {
                    ent.InventTime = DateTime.Parse(table.Rows[0]["InventTime"].ToString());
                }
                if (table.Rows[0]["IsCheck"].ToString() != "")
                {
                    ent.IsCheck = int.Parse(table.Rows[0]["IsCheck"].ToString());
                }
                if (table.Rows[0]["CheckMan"].ToString() != "")
                {
                    ent.CheckMan = int.Parse(table.Rows[0]["CheckMan"].ToString());
                }
                if (table.Rows[0]["CheckTime"].ToString() != "")
                {
                    ent.CheckTime = DateTime.Parse(table.Rows[0]["CheckTime"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Inventory ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "InventoryId");
        }

        public void Update(InventoryEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Inventory set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("InventNum='" + model.InventNum + "',");
            builder.Append("UserId='" + model.UserId + "',");
            builder.Append("InventTime='" + model.InventTime + "',");
            builder.Append("IsCheck=" + model.IsCheck + ",");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("CheckTime='" + model.CheckTime + "',");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where InventoryId=" + model.InventoryId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

